/*
*Script by Icecoldcoke 
*http://ice-scripting.googlecode.com/svn/trunk/
*Icecoldcoke Boss AI - ID 690004 
*/

/* Disabled temporarily.. still figuring out a few things, but all works atm. Remove multi line comments to enable
#include "StdAfx.h"
#include "Setup.h"
class IcecoldcokeBoss : public CreatureAIScript
{
public:
	ADD_CREATURE_FACTORY_FUNCTION(Icecoldcoke);
	SP_AI_Spell spells[3];
 bool m_spellcheck[3];

    IcecoldcokeBoss(Creature* pCreature) : CreatureAIScript(pCreature)
    {
		nrspells = 3; // 3 spells
		for(int i=0;i<nrspells;i++)
		{
			m_spellcheck[i] = false;
		}

		     spells[0].info = dbcSpell.LookupEntry(30600);
		     spells[0].cooldown = 30; // Cooldown (To be changed)
		     spells[0].targettype = TARGET_ATTACKING;// Casts on person attacking
		     spells[0].instant = true; // Instant ? Yes
		     spells[0].perctrigger = (float)RandomFloat(20.0f); // Chance to happen
		     spells[0].attackstoptimer = 3000;
		     m_spellcheck[0] = true;


		     spells[1].info = dbcSpell.LookupEntry(38836);
		     spells[1].cooldown = 5; // Cooldown (To be changed)
		     spells[1].targettype = TARGET_ATTACKING; // Casts on attacker
		     spells[1].instant = true; // Instant ? Yes
		     spells[1].perctrigger = (float)RandomFloat(20.0f); // Chance to happen
		     spells[1].attackstoptimer = 3000;
		     m_spellcheck[1] = true;


		     spells[2].info = dbcSpell.LookupEntry(28524);
		     spells[2].cooldown = 120; // CD (To be changed)
		     spells[2].targettype = TARGET_ATTACKING; // Casts on attacker
		     spells[2].instant = true; // Instant? Yes
		     spells[2].perctrigger = (float)RandomFloat(20.0f); // Chance to happen
		     spells[2].attackstoptimer = 10000;
		     m_spellcheck[2] = true;

  }
    
    void OnCombatStart(Unit* mTarget) // Event on combat starting
    {
		CastTime();
		RegisterAIUpdateEvent(_unit->GetUInt32Value(UNIT_FIELD_BASEATTACKTIME));
    }

	void CastTime()
	{
		for(int i=0;i<nrspells;i++)
			spells[i].casttime = spells[i].cooldown;
	}

	void OnTargetDied(Unit* mTarget)// Event on target death..
    {

    }

    void OnCombatStop(Unit *mTarget) // Event on leaving combat 
    {
		CastTime();
        _unit->GetAIInterface()->setCurrentAgent(AGENT_NULL);
        _unit->GetAIInterface()->SetAIState(STATE_IDLE);
        RemoveAIUpdateEvent();
    }

    void OnDied(Unit * mKiller) // Event after death
    {
		CastTime();
       RemoveAIUpdateEvent();
    }

    void AIUpdate() 
	{
		float val = (float)RandomFloat(100.0f);
		SpellCast(val);
    }

	void SpellCast(float val) // Spell casting info (Not too sure about a lot of this.. still learning the boss AI things, and a lot of this is based off another script.
	{
        if(_unit->GetCurrentSpell() == NULL && _unit->GetAIInterface()->GetNextTarget())
        {
			float comulativeperc = 0;
		    Unit *target = NULL;
			for(int i=0;i<nrspells;i++)
			{
				spells[i].casttime--;
				
				if (m_spellcheck[i])
				{					
					spells[i].casttime = spells[i].cooldown;
					target = _unit->GetAIInterface()->GetNextTarget();
					switch(spells[i].targettype)
					{
						case TARGET_ATTACKING: // Defines who the person attacking is
							_unit->CastSpell(target, spells[i].info, spells[i].instant); break;
					}
					m_spellcheck[i] = false;
					return;
				}

				if ((val > comulativeperc && val <= (comulativeperc + spells[i].perctrigger)) || !spells[i].casttime)
				{
					_unit->setAttackTimer(spells[i].attackstoptimer, false);
					m_spellcheck[i] = true;
				}
				comulativeperc += spells[i].perctrigger;
			}
		}
	}

protected:

	int nrspells;
};

void SetupIcecoldcokeBoss(ScriptMgr * mgr)
{
mgr->register_creature_script(690004, &IcecoldcokeBoss::Create); // Registers script for 690004 
}
*/